from ultralytics import YOLO
import matplotlib

matplotlib.use("TkAgg")
import os

if __name__ == '__main__':
    ptpath = "./ccpddetectnew"
    resultsdet = os.listdir(ptpath)


    with open("val_results_yolov8.txt", "w") as f:
        f.write("YOLO Validation Results:\n\n")

    for m in resultsdet:
        path = os.path.join(ptpath, m, "weights", "best.pt")

        model = YOLO(path)
        results = model.val(
            data="../yaml/data.yaml",
            save_json=True,
            split="val",
            project="./valccpd",
            name="yolov8ssbase"
        )

        # 追加写入当前模型的验证结果
        with open("val_results_yolov8.txt", "a") as f:
            f.write(f"Model: {m}\n")
            f.write(f"Path: {path}\n")
            f.write(str(results) + "\n")
            f.write("-" * 50 + "\n\n")